
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        .msg_box {
            width: 450px;
            background-color: pink;
            text-align: center;
            margin: 0 auto;
        }
        input ,
        textarea {
            width: 400px;
            height: 30px;
            border-radius: 6px;
            border : 1px solid #333 ;
            outline: none; 
            
        }
        textarea {
            height: 100px;
            margin-top: 10px;
            resize: none;
        }
        .message {
            margin-top: 10px;
            margin-left: 350px;
            width: 45px;
            line-height: 20px;
            z-index: 1;
        }
        li {
            list-style: none;
            position: relative;
        }
        .show {
            text-align: left ;
            padding-left: 70px;
        }
        button {
            width: 40px;
            border-radius: 4px;
        }
        h3 {
            width: 50px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            background-color: #ff0;
            border-radius: 50% ;
            position: absolute;
            left: -55px;
            top: 0;
            font-size: 14px;
        }
        h3 ,
        h4 {
            font-weight: normal;
        }
        h4 {
            margin-bottom: 20px;
        }
        p {
            width: 350px;
            height: 30px;
            line-height: 30px;
            background-color: skyblue;
            padding-left: 6px;
            border-radius: 6px;
        }
        div>p {
            margin-top: 5px;
            background-color: salmon;
            margin-bottom: 6px;
            display: block;
        } 
        li button:nth-of-type(1) {
            margin-left: 270px;
        }
        h2 {
            margin-bottom: 20px;
        }
        .cover {
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            background-color: rgba(0,0,0,.5);
            display: flex;
            justify-content: center;
            align-items: center;
            display: none;
        }
        .box {
            width: 450px;
            height: 300px;
            background-color: pink;
            padding-left: 10px;
            margin: auto;
        }
        .box p  {
            margin-bottom: 30px;
            margin: auto;
        }
    </style>
</head>
<body>
    <div class="msg_box">
        <h2>留言板</h2>
        <input type="text" placeholder="昵称" class="nikname"> <br>
        <textarea  placeholder="请输入评论" class="content"></textarea> <br>
        <button class="message">留言</button>
        <div class="show">
            <ul class="list">
                <!-- <li>
                    <h4>2022-4-21 12:12:12</h4>
                    <h3>昵称</h3>
                    <p>留言的内容</p>
                    <div>
                        <p>回复的内容1</p>
                    </div>
                    <button class="answer">回复</button>
                    <button class="delete">删除</button>
                </li> -->
            </ul>
        </div>
       <div class="cover">
            <div class="box">
                <p>请输入回复</p>
                <textarea class="re_content"></textarea> <br>
                <button class="confirm">确认</button>
                <button class="cancel">取消</button>
            </div>
       </div>
   </div>
   
    <script>

        // 拿对象
        const oMsg_box = document.querySelector('.msg_box') ;     //  获取整个留言板
        const oName = document.querySelector('.nikname') ;   //  获取昵称
        const oContent = document.querySelector('.content') ;     //  获取评论内容
        const oMessage = document.querySelector('.message') ;     //  获取留言按钮
        const oUl = document.querySelector('.list') ;     //  获取留言列表
        const oBox = document.querySelector('.cover') ;     //  获取回复弹窗
        const oCon = document.querySelector('.re_content') ;     //  获取回复内容
        let index ; 

        oMsg_box.onclick = function(e) {
            
            const name = oName.value ;
            const content = oContent.value ;

            if(e.target.className === 'message') {
                if(name && content) {
                    const oli = document.createElement('li');
                    oli.innerHTML = `
                   
                        <h4>${dateformat()}</h4>
                        <h3>${name}</h3>
                        <p>${content}</p>
                        <div>
                            
                        </div>
                        <button class="answer">回复</button>
                        <button class="delete">删除</button>
                 
                    ` ;
                    oUl.insertBefore(oli,oUl.firstChild) ;
                    oContent.value = '' ;

                }else {
                    alert('昵称或者评论为空') ;
                }
                return ;
            }

            // 删除
            if(e.target.className === 'delete') {
                e.target.parentNode.remove() ;
                return ;
            }

            //  回复
            if(e.target.className === 'answer') {
                oBox.style.display = 'block';
                index = e.target.previousElementSibling ;
                return ;
            }

            // 取消
            if(e.target.className === 'cancel') {
                oBox.style.display = 'none';
                return ;
            }

            //  确认
            if(e.target.className === 'confirm') {
                const replay = oCon.value ;
                if(replay) {
                    const oP = document.createElement('p') ;
                    oP.innerHTML = replay ;
                    index.appendChild(oP) ;
                    oBox.style.display = 'none';
                    oCon.value = '' ;
                }else {
                    alert('内容为空') ;
                }
            }
        }
        



        const dateformat = () => {
            //  创建日期对象
            const date = new Date() ;
            let y = date.getFullYear() ;
            let m = date.getMonth() + 1 ;
            let day = date.getDate() ;
            let hours = date.getHours() ;
            let min = date.getMinutes() ;
            let s = date.getSeconds() ;
            return `${y}-${arr0(m)}-${arr0(day)} ${arr0(hours)}:${arr0(min)}:${arr0(s)}` ;
        }

        //  补零
        const arr0 =(num) => {
            num = num < 10 ? '0'+ num : num ;
            return num ;
        }

        // console.log(arr0(9)) ;


    </script>
</body>
</html>